package com.foodorder.controller;

import com.foodorder.entity.DeliveryStaff;
import com.foodorder.service.DeliveryStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 配送员控制器
 */
@RestController
@RequestMapping("/api/delivery-staff")
public class DeliveryStaffController {

    @Autowired
    private DeliveryStaffService deliveryStaffService;

    /**
     * 配送员登录
     */
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> loginInfo) {
        String phone = loginInfo.get("phone");
        String password = loginInfo.get("password");
        
        Map<String, Object> result = deliveryStaffService.login(phone, password);
        
        if ((Boolean) result.get("success")) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 获取配送员信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getDeliveryStaff(@PathVariable Long id) {
        Optional<DeliveryStaff> staffOptional = deliveryStaffService.findById(id);
        
        Map<String, Object> response = new HashMap<>();
        
        if (staffOptional.isPresent()) {
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", staffOptional.get());
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "配送员不存在");
            response.put("data", null);
            return ResponseEntity.status(404).body(response);
        }
    }

    /**
     * 更新配送员工作状态
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<Map<String, Object>> updateWorkingStatus(
            @PathVariable Long id,
            @RequestBody Map<String, String> statusInfo) {
        
        String status = statusInfo.get("status");
        DeliveryStaff.WorkingStatus workingStatus;
        
        try {
            workingStatus = DeliveryStaff.WorkingStatus.valueOf(status);
        } catch (IllegalArgumentException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", "无效的工作状态");
            response.put("data", null);
            return ResponseEntity.badRequest().body(response);
        }
        
        try {
            DeliveryStaff staff = deliveryStaffService.updateWorkingStatus(id, workingStatus);
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "状态更新成功");
            response.put("data", staff);
            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 404);
            response.put("message", e.getMessage());
            response.put("data", null);
            return ResponseEntity.status(404).body(response);
        }
    }

    /**
     * 更新配送员位置
     */
    @PutMapping("/{id}/location")
    public ResponseEntity<Map<String, Object>> updateLocation(
            @PathVariable Long id,
            @RequestBody Map<String, String> locationInfo) {
        
        try {
            BigDecimal latitude = new BigDecimal(locationInfo.get("latitude"));
            BigDecimal longitude = new BigDecimal(locationInfo.get("longitude"));
            
            DeliveryStaff staff = deliveryStaffService.updateLocation(id, latitude, longitude);
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "位置更新成功");
            response.put("data", staff);
            return ResponseEntity.ok(response);
        } catch (NumberFormatException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", "无效的位置坐标");
            response.put("data", null);
            return ResponseEntity.badRequest().body(response);
        } catch (IllegalArgumentException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 404);
            response.put("message", e.getMessage());
            response.put("data", null);
            return ResponseEntity.status(404).body(response);
        }
    }

    /**
     * 接受订单
     */
    @PostMapping("/orders/{orderId}/accept")
    public ResponseEntity<Map<String, Object>> acceptOrder(
            @PathVariable Long orderId,
            @RequestBody Map<String, Long> staffInfo) {
        
        Long staffId = staffInfo.get("staffId");
        
        boolean success = deliveryStaffService.acceptOrder(orderId, staffId);
        
        Map<String, Object> response = new HashMap<>();
        
        if (success) {
            response.put("code", 200);
            response.put("message", "接单成功");
            response.put("data", true);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 400);
            response.put("message", "接单失败");
            response.put("data", false);
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 取餐
     */
    @PostMapping("/orders/{orderId}/pickup")
    public ResponseEntity<Map<String, Object>> pickupOrder(
            @PathVariable Long orderId,
            @RequestBody Map<String, Long> staffInfo) {
        
        Long staffId = staffInfo.get("staffId");
        
        boolean success = deliveryStaffService.pickupOrder(orderId, staffId);
        
        Map<String, Object> response = new HashMap<>();
        
        if (success) {
            response.put("code", 200);
            response.put("message", "取餐成功");
            response.put("data", true);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 400);
            response.put("message", "取餐失败");
            response.put("data", false);
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 完成配送
     */
    @PostMapping("/orders/{orderId}/complete")
    public ResponseEntity<Map<String, Object>> completeDelivery(
            @PathVariable Long orderId,
            @RequestBody Map<String, Long> staffInfo) {
        
        Long staffId = staffInfo.get("staffId");
        
        boolean success = deliveryStaffService.completeDelivery(orderId, staffId);
        
        Map<String, Object> response = new HashMap<>();
        
        if (success) {
            response.put("code", 200);
            response.put("message", "配送完成");
            response.put("data", true);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 400);
            response.put("message", "配送完成操作失败");
            response.put("data", false);
            return ResponseEntity.badRequest().body(response);
        }
    }
} 